package impl.gui.util;

import impl.reasoning.ChoiceResult;

import java.awt.Color;
import java.awt.Component;
import java.util.Collection;

import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

import org.semanticweb.owlapi.model.OWLNamedIndividual;

public class ChoiceCellRenderer extends DefaultTableCellRenderer {

	private static final Color				NOT_SELECTED_AND_SATISFYING	= new Color(0xFFAB00);	// light orange
	private static final Color				SELECTED_AND_SATISFIED		= new Color(0xFADF31);	// yellow
	private static final Color				SELECTED_AND_NOT_SATISFYING	= new Color(0xFADF31);	// yellow

	private Collection<OWLNamedIndividual>	indisToColor;

	public ChoiceCellRenderer(Collection<OWLNamedIndividual> indisToColor) {
		this.indisToColor = indisToColor;
	}

	public ChoiceCellRenderer() {

	}

	@Override
	protected void setValue(Object value) {
		if (value instanceof OWLNamedIndividual) {
			OWLNamedIndividual indi = (OWLNamedIndividual) value;
			setText(indi.getIRI().toString());
		} else if (value instanceof ChoiceResult) {
			ChoiceResult uReasoningResult = (ChoiceResult) value;
			setText(Double.toString(uReasoningResult.getUtility()));
		} else {
			setText((value == null) ? "" : value.toString());
		}

	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		if (value instanceof OWLNamedIndividual) {
			if ((this.indisToColor != null) && this.indisToColor.contains(value)) {
				if (isSelected) {
					setBackground(SELECTED_AND_SATISFIED); // satisfying & selected
				} else {
					setBackground(NOT_SELECTED_AND_SATISFYING); // satisfying & not selected
				}
				// setOpaque(true);
			} else if (isSelected) {
				setBackground(SELECTED_AND_NOT_SATISFYING); // not satisfying & selected
				// setOpaque(true);
			} else {
				setBackground(Color.WHITE);
			}
		} else {
			setBackground(Color.WHITE); // not satisfied & not selected
			setBorder(null);
			// setOpaque(true);
		}

		// text alignment
		if (column == 0) {
			setHorizontalAlignment(SwingConstants.LEFT);
		} else if (column == 1) {
			setHorizontalAlignment(SwingConstants.RIGHT);
		}

		setForeground(Color.BLACK);
		setToolTipText("Double-Click to view on Wikipedia");

		return this;
	}
}
